package com.qd.panda.service.space;

import com.cdqidi.dto.ApiResult;
import com.cdqidi.exception.ApiException;
import com.qd.common.panda.domain.entity.space.ParkSpaceTenantDTO;
import com.qd.panda.service.stroke.DisplayUtil;
import com.qd.panda.service.stroke.UserStrokeHolderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.TimeUnit;

/**
 * 〈一句话功能简述〉<br>
 *
 * @author sjk
 * @create 2023-08-18 15:48
 * @since 1.0.0
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class ParkSpaceSyncService {
    private final ParkSpaceTenantService parkSpaceTenantService;
    private final RedissonClient redissonClient;
    private final ParkSpaceHolderService parkSpaceHolderService;

    @Async
    @Transactional
    public void parkSpaceTenant(String bid, Integer payStatus) {
        log.info("短期车位租赁,bid: {}", bid);
        ParkSpaceTenantDTO parkSpaceTenant = parkSpaceTenantService.getByIdDto(bid);
        if (null == parkSpaceTenant) {
            return;
        }
        if (!parkSpaceTenant.getTenantOrderState().equals(DisplayUtil.PAY_STATUS_0)) {
            return;
        }
        if (payStatus == DisplayUtil.PAY_STATUS_1) {
            RLock lock = redissonClient.getLock(parkSpaceTenant.getSid());
            try {
                try {
                    boolean res = lock.tryLock(30, 10, TimeUnit.SECONDS);
                    if (res) {
                        parkSpaceHolderService.updateSuccess(parkSpaceTenant);
                    } else {
                        throw new ApiException(ApiResult.error());
                    }
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    log.error(UserStrokeHolderService.REDIS_LOCKED_ERR, e);
                    throw new ApiException(ApiResult.error());
                }catch (Exception e){
                    log.error("车位租赁",e);
                }
            } finally {
                lock.unlock();
            }
        } else if (payStatus == DisplayUtil.PAY_STATUS_2) {
            parkSpaceHolderService.updateErr(parkSpaceTenant);
        } else {
            log.error("未知的payStatus: {}", payStatus);
        }
    }
    @Async
    public void updateTenantOrderState(String bid, Integer refundStatus) {
        RLock lock = redissonClient.getLock(bid);
        try {
            try {
                boolean res = lock.tryLock(30, 10, TimeUnit.SECONDS);
                if (res) {
                    parkSpaceTenantService.updateTenantOrderState(bid,refundStatus);
                } else {
                    throw new ApiException(ApiResult.error());
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                log.error(UserStrokeHolderService.REDIS_LOCKED_ERR, e);
                throw new ApiException(ApiResult.error());
            }catch (Exception e){
                log.error("车位租赁",e);
            }
        } finally {
            lock.unlock();
        }
    }
}